<?php

/**
 * _Order form.
 *
 * @package    Porba
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class _OrderForm extends Base_OrderForm
{
  public function configure()
  {
      unset($this['created_at'], $this['updated_at']);
      
      //set default values
      $this->setDefault('date_due', time());
      $this->setDefault('date_recieved', time());
      
      //get a list of all customers
      $contacts = ContactTable::findCustomers();
      $newContactArray = array();
      
      //Create a new array with the contact id as the key and the id + name as value
      foreach($contacts as $contact){
          $id = $contact->getId();
          $value = $contact->getContactCode().', '.$contact->getName();
          $newContactArray[$id] = $value;
      }
      
      //Customise the contact_id widget to use the new array
      $this->widgetSchema['contact_id'] = new sfWidgetFormChoice(array(
          'choices' => $newContactArray,
      ));
      
      $this->validatorSchema['contact_id'] = new sfValidatorChoice(array(
          'choices'=>  array_keys($newContactArray),
      ));      
           
  }
 
  public function setCategory($category){
      $this->setDefault('category_name', $category);
  }  
  
  public function setTotalPrice($totalprice){
      $this->setDefault('total_price', $totalprice);
  }
  
  public function getTotalPrice(){
      $total = $this->getDefault('total_price');
      return $total;
  }
  /**
   * Create a widgetschema for the status with only the current status and the next status as choices
   * And set the default status of a stock order to 'in production'
   * @param type $status
   * @param type $category 
   */
  public function customizeStatus($status, $category){
      if($category == 'customer order'){
        if($status == 'quotation' || $status == ''){
            $choices = array();
            $choices['quotation'] = 'quotation';
            $choices['in production'] = 'in production';
            $this->widgetSchema['status'] = new sfWidgetFormChoice(array(
                'choices' => $choices,
            ));

            $this->validatorSchema['status'] = new sfValidatorChoice(array(
                'choices'=>  array_keys($choices),
            ));
        }      
        if($status == 'in production'){
            $choices = array();
            $choices['in production'] = 'in production';
            $choices['ready'] = 'ready';
            $this->widgetSchema['status'] = new sfWidgetFormChoice(array(
                'choices' => $choices,
            ));

            $this->validatorSchema['status'] = new sfValidatorChoice(array(
                'choices'=>  array_keys($choices),
            ));
        }
        if($status == 'ready'){
            $choices = array();
            $choices['ready'] = 'ready';
            $choices['collected'] = 'collected';
            $this->widgetSchema['status'] = new sfWidgetFormChoice(array(
                'choices' => $choices,
            ));

            $this->validatorSchema['status'] = new sfValidatorChoice(array(
                'choices'=>  array_keys($choices),
            ));          
        }
      }
      if($category == 'stock order'){
        if($status == ''){
            $this->setDefault('status', 'in production');
        }         
        if($status == 'in production'){
            $choices = array();
            $choices['in production'] = 'in production';
            $choices['ready'] = 'ready';
            $this->widgetSchema['status'] = new sfWidgetFormChoice(array(
                'choices' => $choices,
            ));

            $this->validatorSchema['status'] = new sfValidatorChoice(array(
                'choices'=>  array_keys($choices),
            ));
        }           
      }
  }
}
